package net.tv90.bfmccore.unlock;

import net.tv90.bfmccore.battlepack.EnumBattlepackLevel;
import net.tv90.bfmccore.util.HUD.HUD;
import net.tv90.bfmccore.util.HUD.HUDDelayedQueue;
import net.tv90.bfmccore.util.HUD.Text;
import net.tv90.bfmccore.util.vector.Vector2i;
import org.bitterorange.flansmodslain.actions.Money;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;

import static net.tv90.bfmccore.battlepack.Battlepack.getBattlepack;

public class UnlockUtil {

    public static void unlockMainWeapon(Player p, String weaponId, int price){
        if(p.hasPermission("unlock.{weaponId}".replace("{weaponId}", weaponId))){
            p.sendMessage("§e[管理员]解锁武器失败，你已经拥有该武器");
            return;
        }
        if(Money.has(p, price) || price == 0){
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                    "lp user {player} permission set unlock.{weaponId} true"
                            .replace("{player}", p.getName())
                            .replace("{weaponId}", weaponId));
            p.playSound(p.getLocation(), "custom.Awards_Unlock", 100, 1);
            p.sendMessage("§e[管理员]解锁武器成功");
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                    "title {player} title 你解锁了该武器"
                            .replace("{player}", p.getName()));
            Money.take(p, price);
        } else {
            p.sendMessage("§e[管理员]解锁武器失败，你没有足够的游戏币");
        }
    }

    public static void unlockGadget(Player p, String weaponId, int price){
        if(p.hasPermission("unlock.{weaponId}".replace("{weaponId}", weaponId))){
            p.sendMessage("§e[管理员]解锁道具失败，你已经拥有该道具");
            return;
        }
        if(Money.has(p, price) || price == 0){
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                    "lp user {player} permission set unlock.{weaponId} true"
                            .replace("{player}", p.getName())
                            .replace("{weaponId}", weaponId));
            p.playSound(p.getLocation(), "custom.Awards_Unlock", 100, 1);
            p.sendMessage("§e[管理员]解锁道具成功");
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                    "title {player} title 你解锁了该道具"
                            .replace("{player}", p.getName()));
            Money.take(p, price);
        } else {
            p.sendMessage("§e[管理员]解锁道具失败，你没有足够的游戏币");
        }
    }

    public static void doNoobTutorial(Player p){
        if(p.hasPermission("bfmc.noob")){
            p.sendMessage("§e[管理员]你已经完成了新手教程，无法重新获得奖励");
            return;
        }
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "lp user {player} permission set bfmc.noob true"
                        .replace("{player}", p.getName()));
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "title {player} title 你已完成新手教程"
                        .replace("{player}", p.getName()));
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "lp user {player} permission set unlock.medkit true"
                        .replace("{player}", p.getName()));
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "lp user {player} permission set unlock.mtn55 true"
                        .replace("{player}", p.getName()));
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "lp user {player} permission set unlock.xm25gl true"
                        .replace("{player}", p.getName()));
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(),
                "lp user {player} permission set unlock.mbtlaw true"
                        .replace("{player}", p.getName()));

        Money.give(p, 5000);
        p.sendMessage("§e[管理员]你已经完成新手教程，获得奖励5000游戏币");
        p.sendMessage("§e[管理员]你已经获得医疗包的解锁权限");
        p.sendMessage("§e[管理员]你已经获得运动传感器的解锁权限");
        p.sendMessage("§e[管理员]你已经获得XM25榴弹发射器的解锁权限");
        p.sendMessage("§e[管理员]你已经获得MBT LAW的解锁权限");

        Text text = new Text(new Vector2i(-1, 120), "新兵", 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(p, "[local]RankIcons/0.png", "custom.promotion", list,
                new Vector2i(-1, 25), new Vector2i(100, 100), 5);
        HUDDelayedQueue.addHUD(p, hud);

        UnlockUtil.showItemHUD(p, "weapon","Medkit");
        UnlockUtil.showItemHUD(p, "weapon","Motion Sensor");
        UnlockUtil.showItemHUD(p, "weapon","XM25 AIRBURST");
        UnlockUtil.showItemHUD(p, "weapon","MBT LAW");

        getBattlepack(p, EnumBattlepackLevel.SILVER);
    }


    /**
     * 给玩家展示物品HUD
     * prefix = attachment/weapon
     * @param p 玩家
     */
    public static void showItemHUD(Player p, String prefix, String item) {
        Text text = new Text(new Vector2i(-1, 135), item, 1.0D);
        List<Text> list = new ArrayList<>();
        list.add(text);
        HUD hud = new HUD(p, "[local]KillInfo/"+prefix+"/" + item + ".png", "custom.Awards_Unlock", list,
                new Vector2i(-1, 20), new Vector2i(110, 110), 3);
        HUDDelayedQueue.addHUD(p, hud);
    }

}
